package com.sun.mybatisplus.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.mybatisplus.bean.User;
import com.sun.mybatisplus.bean.UserExt;
import com.sun.mybatisplus.mapper.UserMapper;
import com.sun.mybatisplus.service.UserExtService;
import com.sun.mybatisplus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserExtService userExtService;

    @Override
//    @Transactional
    public String addData(List<User> users) {

            users.forEach(user -> {
                addUser(user);
            });



        return "success";
    }


    public String addUser(User user) {
        if (user.getName().equals("lisi")) {
            log.info("user的name为lisi,将要抛出异常");
//            int i = 1 / 0;
        }
//        Long id = Long.valueOf(userMapper.insert(user));
        if (!save(user)) {
            throw new RuntimeException("user插入失败");
        }
        UserExt userUserExt = user.getUserExt();
        userUserExt.setUserId(user.getId());
        userExtService.addData(userUserExt);

        return "success";
    }

//    @Cacheable(key = "#id", value = "user")
    @Override
    public User selUserById(Long id) {
        return this.getById(id);
    }
    @Override
    public Map selUserByIdMap(Long id) {
        return userMapper.selUserById(null);
    }


}
